<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Product;
use App\Admin\Repositories\ProductSku;
use App\Models\Category;
use App\Models\Product as ProductModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ProductController extends AdminController
{
    protected $title = '商品';

    public function getProductType()
    {
        return \App\Models\Product::TYPE_NORMAL;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Product(['skus']), function (Grid $grid) {
            $grid->withBorder();

            $grid->model()->with(['category', 'skus']);

            $grid->column('skus', 'SKU')->display(function ($comments) {
                $count = count($comments);

                return "<span class='label label-warning'>{$count}</span>";
            });

            $grid->column('id')->sortable();
            $grid->column('image')->image('', 100);
            $grid->column('type')->using(ProductModel::$typeMap)->label([
                'default'                       => 'primary',
                ProductModel::TYPE_NORMAL       => 'primary',
                ProductModel::TYPE_CROWDFUNDING => 'danger',
            ]);
            // $grid->column('category_id');
            $grid->column('category.title', '类目');
            $grid->column('title');
            $grid->column('long_title');
            $grid->column('on_sale', '已上架')->display(function ($value) {
                return $value ? '是' : '否';
            });
            $grid->column('rating');
            $grid->column('sold_count');
            $grid->column('review_count');
            $grid->column('price');

            $grid->quickSearch(['title'])->placeholder('搜索...');
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $model = Product::with(['category', 'skus']);

        return Show::make($id, $model, function (Show $show) {
            $show->field('id');
            $show->field('type')->using(ProductModel::$typeMap);
            //$show->field('category.title', '商品类目');
            $show->field('full_name', '商品类目')->as(function ($title) {
                // 获取当前行的其他字段
                return $this->category->full_name ?? '';
            });
            $show->field('title');
            $show->field('long_title');
            $show->field('image')->image();;
            $show->field('on_sale');
            $show->field('rating');
            $show->field('sold_count');
            $show->field('review_count');
            $show->field('price');
            $show->field('created_at');
            $show->field('updated_at');
            $show->field('description');
//            $show->skus(function ($model) {
//                $grid = new Grid(new ProductSku());
//
//                $grid->model()->where('product_id', $model->id);
//
//                $grid->id();
//                $grid->column('title', 'SKU 名称');
//                $grid->column('description', 'SKU 描述');
//                $grid->column('price', 'SKU 价格');
//                $grid->column('stock', '库存');
//
//                return $grid;
//            });
//            $show->divider();
//            $show->html('<p><img src="http://shop8.test/storage/products/description/df419c04c95fc7d58cc6f6f34a2fb7fd60d2aa4010b17.jpeg" alt="测试" width="700" height="1244" /></p>');
//            $show->divider();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $categoryModel = \App\Models\Category::class;
        $builder = Product::with('skus');

        return Form::make($builder, function (Form $form) use ($categoryModel) {
            $form->display('id');
            // $form->text('type');
            $form->hidden('type')->value($this->getProductType());
//            $form->text('category_id');
//            $form->select('category_id', '类目')->options(function ($id) {
//                $category = Category::find($id);
//                if ($category) {
//                    return [$category->id => $category->full_name];
//                }
//            })->ajax('/admin/api/categories?is_directory=0');
            $form->select('category_id', '商品类目')->options(function () use ($categoryModel) {
                return $categoryModel::selectOptions();
            })->saving(function ($v) {
                return (int) $v;
            });
            // $form->text('title');
            $form->text('title', '商品名称')->rules('required');
            $form->text('long_title');
            // $form->text('image');
            $form->image('image', '封面图片')->rules('required|image');
            // $form->text('on_sale');
            $form->radio('on_sale', '上架')->options(['1' => '是', '0'=> '否'])->default('0');
            // $form->text('description');
            $form->editor('description')->imageDirectory('products/description');
            // 直接添加一对多的关联模型
            $form->hasMany('skus', 'SKU 列表', function (Form\NestedForm $form) {
                $form->text('title', 'SKU 名称')->rules('required');
                $form->text('description', 'SKU 描述')->rules('required');
                $form->text('price', '单价')->rules('required|numeric|min:0.01');
                $form->text('stock', '剩余库存')->rules('required|integer|min:0');
            });

            // 定义事件回调，当模型即将保存时会触发这个回调
            $form->saving(function (Form $form) {
                $form->model()->price = collect($form->input('skus'))->where(Form::REMOVE_FLAG_NAME, 0)->min('price') ?: 0;
            });
        });
    }
}
